import Treeselect from "@riophae/vue-treeselect/dist/vue-treeselect";

/**
 * 载入项目引导文件
 */
require('./bootstrap');

window.Vue = require('vue');

//分页
import pagination from 'vue-bootstrap-pagination'

const app = new Vue({
    el: '#wrapper',
    data:{
        page_params:page_params,
        cur_article:null,
        article_list:[],//文章列表
        comment_list:[],//评论列表
        box:null,
        articleSearchParams:{//评文章索参数
            keyword:'',
        },
        articlePagination: {
            total: 0,
            per_page: 10,    // required
            current_page: 1, // required
            last_page: 0,    // required
            from: 1,
            to: 12
        },
        commentSearchParams:{//评论搜索参数
            time_range:'none',
            around_comment_type:0,
            keyword:'',
        },
        commentPagination: {
            total: 0,
            per_page: 15,    // required
            current_page: 1, // required
            last_page: 0,    // required
            from: 1,
            to: 12
        },
        paginationOptions: {
            offset: 0,
            previousText: '上一页',
            nextText: '下一页',
            alwaysShowPrevNext: true,
        }
    },
    watch: {
        article_list:function () {
            //重新设置当前文章
            this.resetCurArticle();
        },
        cur_article:function () {
            //重设评论搜索参数
            this.commentSearchParams = {
                time_range:'none',
                    around_comment_type:0,
                    keyword:'',
            };
            this.queryComment();
        },
    },
    mounted:function(){
        this.queryArticle();
    },
    methods: {
        resetCurArticle:function(){//重新设置当前文章
            if (!_.isEmpty(this.article_list)){
                this.cur_article = _.first(this.article_list);
            }
        },
        setCurArticle:function(index){//设置当前选中文章
            //深度拷贝
            this.cur_article = _.cloneDeep(this.article_list[index]);
        },
        queryArticle:function(){//查询文章
            let queryData = _.merge(this.articleSearchParams, {
                page: this.articlePagination.current_page,
                pageSize: this.articlePagination.per_page,
            });
            utils.ajaxGet(page_params.url_article_list, queryData,
                (response, params) => {
                    this.article_list = response.data.data;
                    this.articlePagination.total = response.data.total;
                    this.articlePagination.current_page = response.data.current_page;
                    this.articlePagination.last_page = response.data.last_page;
                }
            );
        },
        queryComment:function(){//查询评论
            let queryData = _.merge(this.commentSearchParams, {
                msg_data_id: this.cur_article.msg_data_id,
                index: this.cur_article.index_num,
                page: this.commentPagination.current_page,
                pageSize: this.commentPagination.per_page,
            });
            utils.ajaxGet(page_params.url_comment_list, queryData,
                (response, params) => {
                    this.comment_list = response.data.data;
                    this.commentPagination.total = response.data.total;
                    this.commentPagination.current_page = response.data.current_page;
                    this.commentPagination.last_page = response.data.last_page;
                }
            );
        },
        open:function (index) {//打开或关闭文章留言
            let article = this.article_list[index];
            utils.ajaxPost(page_params.url_open, {'massArtId':article.id, isOpen:article.open_comment},
                (response, params) => {
                    let data = response.data;
                    utils.showToastr(data);
                    if (data.status == 'success') {
                        this.queryComment();
                    }
                }
            );
        },
        markelect:function (index) {//设置或取消留言精选
            if (page_params.url_markelect) {
                let comment = this.comment_list[index];
                utils.ajaxPost(page_params.url_markelect, {'commentId':comment.id, isMarkelect:!comment.around_comment_type},
                    (response, params) => {
                        let data = response.data;
                        utils.showToastr(data);
                        if (data.status == 'success') {
                            this.queryComment();
                        }
                    }
                );
            }
        },
        deleteComment:function (index) {//删除留言
            let comment = this.comment_list[index];
            utils.confirm(page_params.url_delete, {'commentId':comment.id},
                (response, params) => {
                    let data = response.data;
                    utils.showToastr(data);
                    if (data.status == 'success') {
                        this.queryComment();
                    }
                }
            );
        },
        replyAdd:function (index) {//回复留言
            let comment = this.comment_list[index];
            utils.ajaxPost(page_params.url_replyadd, {'commentId':comment.id,'replyContent':comment.reply_content},
                (response, params) => {
                    let data = response.data;
                    utils.showToastr(data);
                    if (data.status == 'success') {
                        this.queryComment();
                    }
                }
            );
        },
        replyDelete:function (index) {//删除留言回复
            let comment = this.comment_list[index];
            utils.confirm(page_params.url_replydelete, {'commentId':comment.id},
                (response, params) => {
                    let data = response.data;
                    utils.showToastr(data);
                    if (data.status == 'success') {
                        this.queryComment();
                    }
                }
            );
        }
    },
    components: {
        pagination
    },
});